//
// Created by zengao on 2022/12/29.
//

#include "PrintVisitor.h"

void C100::PrintVisitor::visitorProgramNode(C100::ProgramNode *node) {
    for (auto &s: node->stmts) {
        s->accept(this);
    }
}

void C100::PrintVisitor::visitorBinaryNode(C100::BinaryNode *node) {
    node->Lhs->accept(this);
    switch (node->binOp) {
        case BinaryOperator::Sub:
            content += "-";
            break;
        case BinaryOperator::Add:
            content += "+";
            break;
        case BinaryOperator::Mul:
            content += "*";
            break;
        case BinaryOperator::Div:
            content += "/";
            break;
        case BinaryOperator::Lesser:
            content += "<";
            break;
        case BinaryOperator::LesserEqual:
            content += "<=";
            break;
        case BinaryOperator::Equal:
            content += "==";
            break;
        case BinaryOperator::PipeEqual:
            content += "!=";
            break;
        case BinaryOperator::GreaterEqual:
            content += ">=";
            break;
        case BinaryOperator::Greater:
            content += ">";
            break;
        default:
            break;
    }
    node->Rhs->accept(this);
}

void C100::PrintVisitor::visitorConstantNode(C100::ConstantNode *node) {
    content += std::to_string(node->value);
}

void C100::PrintVisitor::visitorExprStmtNode(C100::ExprStmtNode *node) {
    node->Lhs->accept(this);
    content += ";";
}

void C100::PrintVisitor::visitorVarExprNode(C100::VarExprNode *node) {
    content += std::string(node->varObj->name);
}

void C100::PrintVisitor::visitorAssignExprNode(C100::AssignExprNode *node) {
    node->Lhs->accept(this);
    content += '=';
    node->Rhs->accept(this);
}

void C100::PrintVisitor::visitorIfStmtNode(C100::IfStmtNode *node) {
    content+="if";
    content+="(";
    node->Cond->accept(this);
    content+=")";
    node->Then->accept(this);
    if(node->Else){
        content+="else";
        node->Else->accept(this);
    }
}
void C100::PrintVisitor::visitorBlockStmtNode(BlockStmtNode *node){
    content += "{";
    for(auto &s:node->stmts){
        s->accept(this);
    }
    content+="}";
}
